home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Third Party SDKs / ATI RAVE SDK / Samples / RAVEcache / RAVEcache.cpp next >
Encoding:
C/C++ Source or Header  |  1998-07-07  |  9.3 KB  |  404 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    Name:    RAVEcache.cpp
  3.  *
  4.  *  Description: This program shows how to use RAVE to 
  5.  *                 render to an offscreen pixMap, which can 
  6.  *                 be post processed before being BLIT to a
  7.  *                  window using CopyBits().  The approach is
  8.  *                 essentially the following:
  9.  *
  10.  *                    1.  create a RAVE cache context (using 
  11.  *                        the 'kQAContext_Cache' flag)
  12.  *                    2.    register a callback to be called
  13.  *                        when rendering to cache context is
  14.  *                        finished
  15.  *                    3.    render to cache context and call
  16.  *                        render end, which will trigger 
  17.  *                        callback.
  18.  *                    4.    in callback handler, do any desired
  19.  *                        postprocessing of the rendered buffer
  20.  *                        and then wrap the buffer up as a pixMap
  21.  *                        and copy to any window using CopyBits()
  22.  *     
  23.  *  Author:        Chris Bentley 5/5/97
  24.  *
  25.  *  Trade secret of ATI Technologies, Inc.
  26.  *  Copyright 1997, ATI Technologies, Inc., (unpublished)
  27.  *
  28.  *  All rights reserved.  This notice is intended as a precaution against
  29.  *  inadvertent publication and does not imply publication or any waiver
  30.  *  of confidentiality.  The year included in the foregoing notice is the
  31.  *  year of creation of the work.
  32.  */
  33. /****************************************************************/
  34. /* headers                                                        */
  35. /****************************************************************/
  36. #include "stdio.h"
  37. #include "stdlib.h"
  38. #include "string.h"
  39. #include "Quickdraw.h"
  40. #include "RAVE.h"
  41.  
  42. /****************************************************************/
  43. /* defines                                                        */
  44. /****************************************************************/
  45. #define SetGV( v, _x, _y, _z, _invW, _a, _r, _g, _b )        \
  46. {                                                            \
  47.     (v).x = (_x);                                            \
  48.     (v).y = (_y);                                            \
  49.     (v).z = (_z);                                            \
  50.     (v).invW = (_invW);                                        \
  51.     (v).a = (_a);                                            \
  52.     (v).r = (_r);                                            \
  53.     (v).g = (_g);                                            \
  54.     (v).b = (_b);                                            \
  55. }                                                                            
  56.  
  57. /****************************************************************/
  58. /* typedefs                                                        */
  59. /****************************************************************/
  60. typedef unsigned short UINT16;
  61. typedef unsigned long UINT32;
  62.  
  63. /****************************************************************/
  64. /* prototypes                                                    */
  65. /****************************************************************/
  66. void Initialize( void );
  67. int InitWindow( void );
  68. int InitRAVE( void );
  69. TQAEngine *FindEngine( TQADevice *device );
  70. void Cleanup( void );
  71. void callBack( const TQADrawContext    *, const TQADevice *, const TQARect *, void    *);
  72. PixMapHandle alloc_pixmap( UINT32 );
  73. void set_pixmap( PixMapHandle, UINT32, UINT32, UINT32, UINT32, void * );
  74.  
  75. /****************************************************************/
  76. /* global variables                                                */
  77. /****************************************************************/
  78. /*
  79.  * Window variables
  80.  */
  81. Rect             winRect = {40, 40, 240, 240};
  82. WindowPtr         winPtr;
  83.  
  84. /*
  85.  * RAVE variables
  86.  */
  87. GDHandle        gGDevice;
  88. TQADevice       gDevice;
  89. TQAEngine       *gEngine;
  90. TQADrawContext  *gCacheContext;
  91. TQAVGouraud     GVerts[3];
  92. TQARect         gRect;
  93. TQANoticeMethod gNotice;
  94.  
  95. /****************************************************************/
  96. /* code                                                            */
  97. /****************************************************************/
  98. /*
  99.  * Function:    main()
  100.  *
  101.  */
  102. void main( void )
  103. {
  104.     /*
  105.      * setup Mac Toolbox
  106.      */
  107.     Initialize();
  108.     
  109.     /*
  110.      * open window
  111.      */
  112.     if( !InitWindow() )    
  113.          return;
  114.  
  115.     /*
  116.      * init RAVE engine
  117.      */
  118.     if( !InitRAVE() )
  119.     {
  120.         Cleanup();
  121.         return;
  122.     }
  123.     
  124.     /*
  125.      * create a cache context
  126.      */
  127.     if( QADrawContextNew(&gDevice,&gRect,0,gEngine,kQAContext_Cache,&gCacheContext) != kQANoErr )
  128.     {
  129.          printf( "ERROR: Context create failed\n" );
  130.          Cleanup();
  131.          return;
  132.     }
  133.     
  134.     QASetFloat( gCacheContext, kQATag_ColorBG_a, 1.0 );
  135.     QASetFloat( gCacheContext, kQATag_ColorBG_r, 0.2 );
  136.     QASetFloat( gCacheContext, kQATag_ColorBG_g, 0.1 );
  137.     QASetFloat( gCacheContext, kQATag_ColorBG_b, 0.4 );
  138.  
  139.     /*
  140.      * register callback to be called at render end
  141.      */
  142.     gNotice.bufferNoticeMethod = callBack;
  143.     QASetNoticeMethod( gCacheContext, kQAMethod_BufferComposite, gNotice, NULL );
  144.     
  145.     /*
  146.      * render one triangle
  147.      */
  148.     QARenderStart( gCacheContext, NULL, NULL);
  149.  
  150.     //                    x     y    z  invW   a   r    g    b
  151.     SetGV( GVerts[0], 10.0,    10.0,10.0,1.0, 0.5, 1.0, 0.0, 0.0 );
  152.     SetGV( GVerts[1], 150.0,10.0,10.0,1.0, 0.2, 0.0, 0.8, 0.0 );
  153.     SetGV( GVerts[2], 75.0,100.0,10.0,1.0, 0.1, 0.0, 0.0, 0.7 );    
  154.     QADrawTriGouraud(gCacheContext, &GVerts[0], &GVerts[1], &GVerts[2], kQATriFlags_None );
  155.  
  156.     QARenderEnd( gCacheContext, NULL );
  157.     
  158.     /*
  159.      * end
  160.      */
  161.     Cleanup();
  162. }
  163.  
  164. /*
  165.  * Function:    callBack()
  166.  *
  167.  */
  168. void callBack(     const TQADrawContext    *drawContext,        /* Draw context */
  169.                 const TQADevice            *buffer,            /* TQADevice describing back buffer */
  170.                 const TQARect            *dirtyRect,            /* Minimum area to process; NULL means whole buffer */
  171.                 void                    *refCon )
  172. {
  173.     long                 rowBytes     = buffer->device.memoryDevice.rowBytes;
  174.     TQAImagePixelType     pixelType    = buffer->device.memoryDevice.pixelType;
  175.     long                 width        = buffer->device.memoryDevice.width;
  176.     long                 height        = buffer->device.memoryDevice.height;
  177.     void                 *baseAddr    = buffer->device.memoryDevice.baseAddr;
  178.     UINT32                pixSize     = (pixelType == kQAPixel_ARGB32) ? 32 : 16;
  179.     Rect                srcBounds;
  180.     PixMapHandle         hSrcPix;
  181.     UINT32                i, x, y;
  182.     
  183.     /*
  184.      * mess with the pixMap memory
  185.      */
  186.     if( pixSize == 16 )
  187.     {
  188.         UINT16 *dataPtr = (UINT16 *)baseAddr;
  189.         
  190.         for( i = 0; i < 100; i++ )
  191.         {
  192.             x = Random()%width;
  193.             y = Random()%height;
  194.             
  195.             *(dataPtr+(y*width)+x) = 0xFFFFFFFF;
  196.         }
  197.     }
  198.     else
  199.     {
  200.         UINT32 *dataPtr = (UINT32 *)baseAddr;
  201.         
  202.         for( i = 0; i < 100; i++ )
  203.         {
  204.             x = Random()%width;
  205.             y = Random()%height;
  206.             
  207.             *(dataPtr+(y*width)+x) = 0xFFFF;
  208.         }
  209.     }
  210.     
  211.     /*
  212.      * wrap kQADeviceMemory in PixMap and use
  213.      * CopyBits() to BLIT to window
  214.      */
  215.     if( (hSrcPix = alloc_pixmap( pixSize )) == NULL )
  216.         return;
  217.      
  218.     set_pixmap( hSrcPix, rowBytes, width, height, pixSize, baseAddr );
  219.     SetRect( &srcBounds, 0, 0, width, height );
  220.                                  
  221.     ForeColor( blackColor );
  222.     BackColor( whiteColor );
  223.     
  224.     SetPort( winPtr );
  225.                     
  226.     CopyBits( (BitMap *)*hSrcPix, 
  227.               (BitMap *)(*(((CGrafPtr)winPtr)->portPixMap)), 
  228.               &srcBounds, 
  229.               &winPtr->portRect, 
  230.               srcCopy, 
  231.               NULL );
  232.               
  233.     DisposePixMap( hSrcPix );
  234. }
  235.  
  236. /*
  237.  * Function:    alloc_pixmap()
  238.  *
  239.  */
  240. PixMapHandle alloc_pixmap( UINT32 u32depth )
  241. {
  242.     PixMapHandle    hPixMap;
  243.     CTabHandle        hcTab;
  244.     
  245.     if( (hPixMap = NewPixMap()) == NULL )
  246.         return( NULL );
  247.     
  248.     hcTab = (CTabHandle)NewHandleClear( 8L );
  249.     (*hcTab)->ctSeed = u32depth;
  250.     (*hcTab)->ctFlags = 0x0000;
  251.     
  252.     DisposeHandle( (char **)(*hPixMap)->pmTable );
  253.     (*hPixMap)->pmTable = hcTab;
  254.     
  255.     return( hPixMap );
  256. }
  257.  
  258. /*
  259.  * Function:    set_pixmap()
  260.  *
  261.  */
  262. void set_pixmap( PixMapHandle pix_hdl, UINT32 rowBytes, UINT32 u32width, UINT32 u32height, UINT32 u32depth, void *data )
  263. {
  264.     SetRect( &(**pix_hdl).bounds, 0, 0, u32width, u32height );
  265.     (**pix_hdl).pixelSize     = u32depth;
  266.     (**pix_hdl).rowBytes     = rowBytes | 0x8000;
  267.     (**pix_hdl).baseAddr     = (Ptr)data;
  268.     
  269.     (**pix_hdl).pixelType     = 16;                    /* Direct Pixel Type (i.e. non-indexed)*/
  270.     (**pix_hdl).cmpCount     = 3;
  271.     (**pix_hdl).cmpSize     = ( (u32depth == 32) ? 8 : 5 );
  272.     
  273.     (**pix_hdl).packType     = 0;
  274.     (**pix_hdl).packSize     = 0;
  275.     (**pix_hdl).planeBytes     = 0L;
  276.     (**pix_hdl).pmTable     = NULL;
  277.     
  278.     (**pix_hdl).pmVersion     = 0;
  279. }
  280.  
  281. /*
  282.  * Function:    Initialize()
  283.  *
  284.  */
  285. void Initialize( void )
  286. {
  287.     OSErr        error;
  288.     SysEnvRec    theWorld;
  289.         
  290.     /*
  291.      *    Test the computer to be sure we can do color.  
  292.      *    If not we would crash, which would be bad.  
  293.      *    If we can’t run, just beep and exit.
  294.      */
  295.     error = SysEnvirons( 1, &theWorld );
  296.     if( theWorld.hasColorQD == false ) 
  297.         ExitToShell();
  298.     
  299.     /* 
  300.      * Initialize all the needed managers. 
  301.      */
  302.     InitGraf( &qd.thePort );
  303.     InitFonts();
  304.     InitWindows();
  305.     InitMenus();
  306.     TEInit();
  307.     InitDialogs(nil);
  308.     InitCursor();
  309.     
  310.     GetDateTime((unsigned long*) &qd.randSeed);
  311. }
  312.  
  313. /*
  314.  * Function:    InitWindow()
  315.  *
  316.  */
  317. int InitWindow( void )
  318. {
  319.     if( (winPtr = NewCWindow( NULL, &winRect, "\pRAVE", true, 
  320.                                  noGrowDocProc, (WindowPtr)-1, false, 0 )) == NULL )
  321.     {
  322.         printf( "ERROR: opening window\n" );
  323.         return( false );
  324.     }
  325.     
  326.     return( true );
  327. }
  328.  
  329. /*
  330.  * Function:    InitRAVE()
  331.  *
  332.  */
  333. int InitRAVE( void )
  334. {    
  335.     if( (gGDevice = GetMainDevice()) == NULL )
  336.     {
  337.          printf( "ERROR: No monitors found\n" );
  338.          return( false );
  339.     }
  340.  
  341.     gDevice.deviceType         = kQADeviceGDevice;
  342.     gDevice.device.gDevice     = gGDevice;
  343.  
  344.     if( (gEngine = FindEngine( &gDevice )) == NULL )
  345.     {
  346.          printf( "ERROR: No Engine available\n" );
  347.          return( false );
  348.     }
  349.  
  350.     if( QAEngineCheckDevice( gEngine, &gDevice ) )
  351.     {
  352.          printf( "ERROR: EngineCheckDevice failed\n" );
  353.          return( false );
  354.     }
  355.  
  356.     gRect.left = winRect.left; gRect.right  = winRect.right;
  357.     gRect.top  = winRect.top;  gRect.bottom = winRect.bottom;
  358.  
  359.     return( true );
  360. }
  361.  
  362. /*
  363.  * Function:    FindEngine()
  364.  *
  365.  */
  366. TQAEngine *FindEngine( TQADevice *device )
  367. {
  368.     TQAEngine     *tmpEngine;
  369.     UINT32        vendorID;
  370.  
  371.     /*
  372.      * try to find ATI RAVE engine
  373.      */
  374.     for(     tmpEngine = QADeviceGetFirstEngine(device);
  375.             tmpEngine;
  376.             tmpEngine = QADeviceGetNextEngine(device, tmpEngine) )
  377.     {
  378.         QAEngineGestalt( tmpEngine, kQAGestalt_VendorID, &vendorID );        
  379.         if( vendorID == 1 )
  380.             return( tmpEngine );
  381.     }
  382.  
  383.     /*
  384.      * if no ATI RAVE engine, go with first engine available
  385.      */
  386.     return( QADeviceGetFirstEngine(device) );
  387. }
  388.  
  389. /*
  390.  * Function:    Cleanup()
  391.  *
  392.  */
  393. void Cleanup( void )
  394. {
  395.     while( !Button() )
  396.         ;
  397.  
  398.     if( gCacheContext )
  399.         QADrawContextDelete( gCacheContext );
  400.             
  401.     if( winPtr )
  402.         DisposeWindow( winPtr );
  403. }
  404.